import cv2
import fire
from math import *
import numpy as np


class ImageUtils:
    ## 在图像的指定区域标记上指定颜色 并保存输出
    def fillmaterial(self, srcFilePath, color, startarea, endarea, output):
        img = cv2.imread(srcFilePath, cv2.IMREAD_COLOR)
        cv2.rectangle(img, startarea, endarea, color, -1)
        # cv2.namedWindow('lena', cv2.WINDOW_AUTOSIZE)
        # cv2.imshow('lena', img)
        cv2.imwrite(output, img)

    ## 旋转图像指定角度+-360度
    def rotation(self, srcFilePath, degree, output):
        img = cv2.imread(srcFilePath)

        height, width = img.shape[:2]

        # 旋转后的尺寸
        heightNew = int(width * fabs(sin(radians(degree))) + height * fabs(cos(radians(degree))))
        widthNew = int(height * fabs(sin(radians(degree))) + width * fabs(cos(radians(degree))))

        matRotation = cv2.getRotationMatrix2D((width / 2, height / 2), degree, 1)

        matRotation[0, 2] += (widthNew - width) / 2  # 重点在这步，目前不懂为什么加这步
        matRotation[1, 2] += (heightNew - height) / 2  # 重点在这步

        imgRotation = cv2.warpAffine(img, matRotation, (widthNew, heightNew), borderValue=(255, 255, 255))

        cv2.imwrite(output, imgRotation)
        # cv2.namedWindow('img',0)
        # cv2.resizeWindow('img',900,900)
        # cv2.imshow("img", img)
        # cv2.namedWindow('imgRotation', 0)
        # cv2.resizeWindow('imgRotation', 900, 900)
        # cv2.imshow("imgRotation", imgRotation)
        # cv2.waitKey(0)

    def resize(self, src, output, ratio=1):
        image = cv2.imread(src)
        h = image.shape[0]
        w = image.shape[1]
        md = image.shape[2]
        dh = int(h * ratio)
        dw = int(w * ratio)
        dst = cv2.resize(image, (dw, dh))
        # cv2.imshow('image',dst)
        # cv2.waitKey(0)
        cv2.imwrite(output, dst)
if __name__ == '__main__':
    fire.Fire(ImageUtils)
    # ImageUtils.resize(ImageUtils,"D:\\archives\\home\\upload\\test1\\Test01.jpg","D:\\0111.jpg",0.1)
